package Communications;

import GUI.MainFrame;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 */
public class SendData {

    static OutputStream outStream;
    static InputStream inStream;
    static InputStreamReader inStreamRead;
    static OutputStreamWriter outStreamWrite;
    byte [] buff = new byte[500];
    int messageLength=0;
    private Socket s = new Socket(Settings.serverName,Settings.portNumber);
    private String message = "";
    private String inMessage;

    public SendData() throws IOException {

        outStream      = new BufferedOutputStream(s.getOutputStream());
        outStreamWrite = new OutputStreamWriter(outStream, "US-ASCII");
        inStream = new BufferedInputStream((s.getInputStream()));
        inStreamRead = new InputStreamReader(inStream, "US-ASCII");


    }

    public char sendData(){

        char connected;
        try{
            connected = (char)inStream.read();

        }catch(Exception e){
            connected = 'N';
            e.printStackTrace();
       }
        return connected;
    }
    public String sendData(String id,String name) throws IOException {

        message += MainFrame.state + "|L|"+id+"|"+name+"|%";

         writeMessage(message);
         message="";
        try {

            inStream = s.getInputStream();
            messageLength = inStream.read(buff);
            inMessage = new String(buff, 0, messageLength);
           // System.out.println(inMessage);
            return inMessage;
            //Need to process "message" string to see what is the second char in the string.
            //depending on that char direct the sequence to a different function

        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }
    public void sendData(char criteria,String value) throws IOException {

        message += MainFrame.state + "|S|"+criteria+"|"+value+"|%";

         writeMessage(message);
         message="";
       /* try {

            inStream = s.getInputStream();
            messageLength = inStream.read(buff);
            inMessage = new String(buff,0,messageLength);
            System.out.println(inMessage);


            //Need to process "message" string to see what is the second char in the string.
            //depending on that char direct the sequence to a different function

        } catch (Exception e) {
            e.printStackTrace();
        }    */

    }
    public void sendData(String name,String surname,String address) throws IOException{

        message += MainFrame.state + "|R|"+name+"|"+surname+"|"+address+"|%";

        writeMessage(message);

        message="";

        /*try {

            inStream = s.getInputStream();
            messageLength = inStream.read(buff);
            inMessage = new String(buff,0,messageLength);
            System.out.println(inMessage);

            //Need to process "message" string to see what is the second char in the string.
            //depending on that char direct the sequence to a different function

        } catch (Exception e) {
            e.printStackTrace();
        } */

    }

    private void writeMessage(String message) throws IOException {

        outStream = s.getOutputStream();

        outStream.write(message.getBytes());
    }
}
